/*
 * @lc app=leetcode.cn id=438 lang=golang
 *
 * [438] 找到字符串中所有字母异位词
 */

// @lc code=start
func findAnagrams(s string, p string) []int {
	sLen, plen := len(s), len(p)
	ans := []int{}
	if sLen < plen {
		return ans
	}
	sCount, pCount := [26]int{}, [26]int{}
	for i, v := range p {
		pCount[v-'a']++
		sCount[s[i]-'a']++
	}
	if sCount == pCount {
		ans = append(ans, 0)
	}
	for l, r := 1, plen; r < sLen-plen; r++ {
		sCount[s[r]-'a']++
		sCount[s[l]-'a']--
		l++

		if sCount == pCount {
			ans = append(ans, r)
		}
	}
	return ans

}

// @lc code=end

